package com.bmadmin.controller.form;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bmadmin.domain.Datagrid;
import com.bmadmin.domain.NormalResult;
import com.bmadmin.domain.SjkcxDomain;
import com.bmadmin.domain.SysUsersPOJO;
import com.bmadmin.domain.XtqxDomain;
import com.bmadmin.service.SjkCxService;
import com.bmadmin.service.XtqxService;
import com.bmadmin.util.BMUtil;
import com.bmadmin.util.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/xtqx")
public class XtqxController {
	private Logger logger = LogManager.getLogger(this.getClass().getName());

	@Autowired
    XtqxService xtqxService;
    @Autowired
    SjkCxService sjkcxService;
    /**
     * 获取数据库查询申请表列表
     */
	@GetMapping("/getXtqxList")
    public Object getXtqxList(HttpSession session,XtqxDomain XtqxDomain,Integer page,Integer limit) {
    	Datagrid result = new Datagrid();
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	
    	Map map = new HashMap();
		map.put("qs_date", XtqxDomain.getQs_date());
		map.put("jz_date", XtqxDomain.getJz_date());
		if(XtqxDomain.getReq_state()==null){
			map.put("req_state", "0,3,4");//默认显示表单状态 0--未提交 3--审核未通过 4--等待验证
		}else if("".equals(XtqxDomain.getReq_state())){
			map.put("req_state", "0,1,2,3,4,5");
		}else{
			map.put("req_state", XtqxDomain.getReq_state());
		}
		map.put("userid", user.getU_id());
		logger.info(XtqxDomain);//列表条件输出
		
		PageHelper.startPage(page, limit);
		List<XtqxDomain> list = xtqxService.getXtqxList(map);
		PageInfo pagelist = new PageInfo(list);
		
		result.setCode(0);
        result.setMsg("success");
        result.setData(list);
        result.setCount(pagelist.getTotal());

        return result;
    }
	
	
    /**
     * 获取登录人信息
     */
    @GetMapping("/getdlrxx")
    public NormalResult getdlrxx(HttpSession session){
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	Map map = new HashMap();
    	map.put("u_id", user.getU_id());
    	SjkcxDomain sjkcxdomain = sjkcxService.getDlrXx(map);
    	sjkcxdomain.setReq_apply_date(BMUtil.getNowTimeForString());
        NormalResult result = new NormalResult();
        result.setCode("0000");
        result.setMsg("success");
        result.setData(sjkcxdomain);
        return result;
    }
    
    /**
     * 申请单保存
     */
    @PostMapping("/insertXtqx")
    public NormalResult insertXtqx(HttpSession session,XtqxDomain XtqxDomain) {
    	String log = "系统权限申请单保存>>>>>>>>>>>";
    	logger.info(log+"参数【"+XtqxDomain+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(XtqxDomain.getReq_system_name())){
        		result.setCode("9999");
                result.setMsg("系统信息名称为空！");
                logger.info(log+"系统信息名称为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(XtqxDomain.getReq_des())){
        		result.setCode("9999");
                result.setMsg("系统权限描述为空！");
                logger.info(log+"系统权限描述为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(XtqxDomain.getReq_apply_phone())){
        		result.setCode("9999");
                result.setMsg("联系电话为空！");
                logger.info(log+"联系电话为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(XtqxDomain.getReq_apply_type())){
        		result.setCode("9999");
                result.setMsg("申请类型为空！");
                logger.info(log+"申请类型为空！");
                return result;
        	}

        	XtqxDomain.setReq_apply_userid(user.getU_id());
        	XtqxDomain.setReq_apply_depid(user.getD_id());
        	XtqxDomain.setReq_apply_date(BMUtil.getNowTimeForString());
        	int retResult = xtqxService.insertXtqx(XtqxDomain);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("保存成功！");
                logger.info(log+"保存成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("保存失败！");
                logger.info(log+"保存失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("申请单保存异常！");
            logger.error(e);
        }
        return result;
    }
    
    /**
     * 申请单删除
     */
    @PostMapping("/deleteXtqx")
    public NormalResult deleteXtqx(XtqxDomain XtqxDomain) {
        NormalResult result = new NormalResult();
        if (BMUtil.isNullOrEmpty(XtqxDomain.getReq_id())) {
            result.setCode("9999");
            result.setMsg("传入申请单ID为空!");
            return result;
        }

        result = xtqxService.deleteXtqx(XtqxDomain.getReq_id());
        return result;
    }
    
    /**
     * 获取申请单信息
     */
    @GetMapping("/getOneXtqx")
    public NormalResult getOneXtqx(HttpSession session,String req_id){
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	NormalResult result = new NormalResult();
    	if (BMUtil.isNullOrEmpty(req_id)) {
            result.setCode("9999");
            result.setMsg("传入申请单ID为空!");
            return result;
        }
    	Map map = new HashMap();
//    	map.put("userid", user.getU_id());
    	map.put("req_id",Integer.parseInt(req_id));
    	List<XtqxDomain> list = xtqxService.getXtqxList(map);
    	XtqxDomain xtqxDomain = list.get(0);
       
        result.setCode("0000");
        result.setMsg("success");
        result.setData(xtqxDomain);
        return result;
    }
    
    /**
     * 申请单更新
     */
    @PostMapping("/updateXtqx")
    public NormalResult updateXtqx(HttpSession session,XtqxDomain XtqxDomain) {
    	String log = "系统权限申请单更新>>>>>>>>>>>";
    	logger.info(log+"参数【"+XtqxDomain+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(XtqxDomain.getReq_id())){
        		result.setCode("9999");
                result.setMsg("申请单ID为空！");
                logger.info(log+"申请单ID为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(XtqxDomain.getReq_system_name())){
        		result.setCode("9999");
                result.setMsg("系统信息名称为空！");
                logger.info(log+"系统信息名称为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(XtqxDomain.getReq_des())){
        		result.setCode("9999");
                result.setMsg("系统权限描述为空！");
                logger.info(log+"系统权限描述为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(XtqxDomain.getReq_apply_phone())){
        		result.setCode("9999");
                result.setMsg("联系电话为空！");
                logger.info(log+"联系电话为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(XtqxDomain.getReq_apply_type())){
        		result.setCode("9999");
                result.setMsg("申请类型为空！");
                logger.info(log+"申请类型为空！");
                return result;
        	}
        	XtqxDomain.setReq_apply_userid(user.getU_id());
        	XtqxDomain.setReq_apply_depid(user.getD_id());
        	XtqxDomain.setReq_apply_date(BMUtil.getNowTimeForString());
        	int retResult = xtqxService.updateXtqx(XtqxDomain);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("更新成功！");
                logger.info(log+"更新成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("更新失败！");
                logger.info(log+"更新失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("申请单更新异常！");
            logger.error(e);
        }
        return result;
    }
    
    /**
     * 申请单提交
     */
    @PostMapping("/upXtqx")
    public NormalResult upXtqx(XtqxDomain XtqxDomain) {
    	NormalResult result = new NormalResult();
    	try {
	        if (BMUtil.isNullOrEmpty(XtqxDomain.getReq_id())) {
	            result.setCode("9999");
	            result.setMsg("传入申请单ID为空!");
	            return result;
	        }
	        XtqxDomain.setReq_state("1");
	        result = xtqxService.upXtqx(XtqxDomain);
	        return result;
    	}catch (Exception e) {
            result.setCode("9999");
            result.setMsg("申请单提交异常！");
            logger.error(e);
            return result;
        }        
    }
    
    /**
     * 申请单确认
     */
    @PostMapping("/sureXtqx")
    public NormalResult sureXtqx(HttpSession session,XtqxDomain XtqxDomain) {
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	NormalResult result = new NormalResult();
    	try {
	        if (BMUtil.isNullOrEmpty(XtqxDomain.getReq_id())) {
	            result.setCode("9999");
	            result.setMsg("传入申请单ID为空!");
	            return result;
	        }
    		if (BMUtil.isNullOrEmpty(XtqxDomain.getReq_sure_view())) {
	            result.setCode("9999");
	            result.setMsg("申请人确认情况为空!");
	            return result;
	        }
    		XtqxDomain.setReq_sure_userid(user.getU_id());
    		XtqxDomain.setReq_sure_date(BMUtil.getNowTimeForString());
	        XtqxDomain.setReq_state("9");
	        result = xtqxService.sureXtqx(XtqxDomain);
	        return result;
    	}catch (Exception e) {
            result.setCode("9999");
            result.setMsg("申请单确认异常！");
            logger.error(e);
            return result;
        }        
    }
    
    /**
     * 获取数据库查询申请表审核列表
     */
	@GetMapping("/getXtqxShList")
    public Object getXtqxShList(HttpSession session,XtqxDomain XtqxDomain,Integer page,Integer limit) {
    	Datagrid result = new Datagrid();
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	
    	Map map = new HashMap();
		map.put("qs_date", XtqxDomain.getQs_date());
		map.put("jz_date", XtqxDomain.getJz_date());
		if(XtqxDomain.getReq_state()==null){
			map.put("req_state", "1");//默认显示表单状态 1--已提交未审核
		}else if("".equals(XtqxDomain.getReq_state())){
			map.put("req_state", "1,2,3,4,5");
		}else{
			map.put("req_state", XtqxDomain.getReq_state());
		}
		map.put("depid", user.getD_id());
		logger.info(XtqxDomain);//列表条件输出
		
		PageHelper.startPage(page, limit);
		List<XtqxDomain> list = xtqxService.getXtqxList(map);
		PageInfo pagelist = new PageInfo(list);
		
		result.setCode(0);
        result.setMsg("success");
        result.setData(list);
        result.setCount(pagelist.getTotal());

        return result;
    }
	
    /**
     * 申请单审核
     */
    @PostMapping("/verifyXtqx")
    public NormalResult verifyXtqx(HttpSession session,XtqxDomain XtqxDomain) {
    	String log = "系统权限申请单审核>>>>>>>>>>>";
    	logger.info(log+"参数【"+XtqxDomain+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(XtqxDomain.getReq_id())){
        		result.setCode("9999");
                result.setMsg("申请单ID为空！");
                logger.info(log+"申请单ID为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(XtqxDomain.getReq_verify_view())){
        		result.setCode("9999");
                result.setMsg("申请部门意见为空！");
                logger.info(log+"申请部门意见为空！");
                return result;
        	}
        	
        	XtqxDomain.setReq_verify_userid(user.getU_id());
        	XtqxDomain.setReq_verify_date(BMUtil.getNowTimeForString());
        	int retResult = xtqxService.verifyXtqx(XtqxDomain);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("审核成功！");
                logger.info(log+"审核成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("审核失败！");
                logger.info(log+"审核失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("申请单审核异常！");
            logger.error(e);
        }
        return result;
    }
    
    /**
     * 获取数据库查询申请表处理列表
     */
	@GetMapping("/getXtqxClList")
    public Object getXtqxClList(HttpSession session,XtqxDomain XtqxDomain,Integer page,Integer limit) {
    	Datagrid result = new Datagrid();
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	
    	Map map = new HashMap();
		map.put("qs_date", XtqxDomain.getQs_date());
		map.put("jz_date", XtqxDomain.getJz_date());
		if(XtqxDomain.getReq_state()==null){
			map.put("req_state", "2,5");//默认显示表单状态 2--审核通过未处理 5--验证未通过 
		}else if("".equals(XtqxDomain.getReq_state())){
			map.put("req_state", "2,4,5");
		}else{
			map.put("req_state", XtqxDomain.getReq_state());
		}
		logger.info(XtqxDomain);//列表条件输出
		
		PageHelper.startPage(page, limit);
		List<XtqxDomain> list = xtqxService.getXtqxList(map);
		PageInfo pagelist = new PageInfo(list);
		
		result.setCode(0);
        result.setMsg("success");
        result.setData(list);
        result.setCount(pagelist.getTotal());

        return result;
    }
	
    /**
     * 申请单处理
     */
    @PostMapping("/disposeXtqx")
    public NormalResult disposeXtqx(HttpSession session,XtqxDomain XtqxDomain) {
    	String log = "系统权限申请单处理>>>>>>>>>>>";
    	logger.info(log+"参数【"+XtqxDomain+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(XtqxDomain.getReq_id())){
        		result.setCode("9999");
                result.setMsg("申请单ID为空！");
                logger.info(log+"申请单ID为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(XtqxDomain.getReq_dispose_view())){
        		result.setCode("9999");
                result.setMsg("系统管理员处理情况为空！");
                logger.info(log+"系统管理员处理情况为空！");
                return result;
        	}
        	
        	XtqxDomain.setReq_dispose_userid(user.getU_id());
        	XtqxDomain.setReq_dispose_date(BMUtil.getNowTimeForString());
        	int retResult = xtqxService.disposeXtqx(XtqxDomain);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("处理成功！");
                logger.info(log+"处理成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("处理失败！");
                logger.info(log+"处理失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("申请单处理异常！");
            logger.error(e);
        }
        return result;
    }
	
}
